package charlie.shop.warehouse;


import charlie.shop.misc.Common;
import com.charlie.beans.ShipmentOrder;
import com.charlie.sessions.ShipmentOrderFacadeLocal;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Show all available orders
 * 
 * @author Alexandra
 */
@WebServlet(name = "ViewOrders", urlPatterns = {"/warehouse/orders"})
public class ViewOrders extends HttpServlet {
    
    /**
     * Items per page limit static constant
     */
    private static final int ITEMS_PER_PAGE = 20;
    private static final Integer QUEUE_STATUS = 1;
    
    @EJB
    private ShipmentOrderFacadeLocal shipmentOrder;

    @Override
    public void init() throws ServletException {
        super.init();
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8"); 
        int page = Common.getPage(request);
        int itemsCount = shipmentOrder.countByID(QUEUE_STATUS);
        int pagesCount = (int) Math.ceil((float) itemsCount / (float) ITEMS_PER_PAGE);
      
         List<ShipmentOrder> orders = null;

       if (page <= pagesCount) {
          orders =shipmentOrder.findByStatus(QUEUE_STATUS,(page - 1) * ITEMS_PER_PAGE, page * ITEMS_PER_PAGE);
       }
        
        getServletContext().setAttribute("orderList", orders);
        request.setAttribute("page", page);
        request.setAttribute("pagesCount", pagesCount);
        getServletContext().getRequestDispatcher("/warehouse/listOfOrders.jsp").forward(request, response);
    }
}
